/* Copyright (C) 2003, Charles Wang.
 * Author:  Charles Wang <charles@linux.net.cn>
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANT; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public LIcense for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307, USA.
 */
#include  <sys/types.h>
#include  <sys/stat.h>
#include  <dirent.h>
#include  <errno.h>
#include  <string.h>
#include  <stdio.h>
#include  <stdlib.h>
#include  <unistd.h>
#include  <newt.h>
#include  "magicbooter.h"
#include  "newtutils.h"

char  instlang[32];

static int search_langset(char *_langset);

const char * langset_runner(void) {
#if !defined(NONEWT)
    char _langset[512], *lscur;
    newtGrid       grid;
    newtComponent  form, label, listbox, button;
#endif
    char    linkbuf[128];
    char  * result;

    if (search_langset(_langset) < 2) {
	/* Only one language is available, so no need to distrib the user. */
	return  NULL;
    }

#if !defined(NONEWT)
    if (!FLAG_TEST(EXPERT))
        result = strtok(_langset, ":");
    else {
        grid = newtCreateGrid(1, 3);
        label = newtLabel(-1, -1, "Please choose the language used in the installation:");
        listbox = newtListbox(-1, -1, 8, NEWT_FLAG_BORDER | NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
        for (lscur = strtok(_langset, ":"); lscur != NULL; lscur = strtok(NULL, ":"))
            newtListboxAppendEntry(listbox, lscur, lscur);
        button = newtButton(-1, -1, "Choose");

        newtGridSetField(grid, 0, 0, NEWT_GRID_COMPONENT, label,
                         1, 0, 1, 0, 0, 0);
        newtGridSetField(grid, 0, 1, NEWT_GRID_COMPONENT, listbox,
                         1, 1, 1, 0, 0, 0);
        newtGridSetField(grid, 0, 2, NEWT_GRID_COMPONENT, button,
                         1, 1, 1, 0, 0, 0);

        form = newtExtForm(NULL, NULL, 0);
        newtGridAddComponentsToForm(grid, form, 1);

        newtGridWrappedWindow(grid, "Language");
        newtGridFree(grid, 1);

        newtExtRunForm(form);
        result = (char *)newtListboxGetCurrent(listbox);
        newtFormDestroy(form);
        newtPopWindow();
    }
#else                         /* NONEWT */
    /* langset is set in spec.c */
    /* Use the first language as preferred. */
    result = strtok(_langset, ":");
#endif

    strcpy(instlang, result);

    snprintf(linkbuf, sizeof(linkbuf), "/etc/sysconfig/i18n.%s", result);
#ifdef USE_UDEV
    if (symlink(linkbuf, "/etc/sysconfig/i18n"))
	newtWinMessage("Error", "symlink(%s, /etc/sysconfig/i18n) failed: %s",
		       linkbuf, strerror(errno));
#else
    if (symlink(linkbuf, "/etc/i18n"))
	newtWinMessage("Error", "symlink(%s, /etc/i18n) failed: %s",
		       linkbuf, strerror(errno));
#endif
    return  NULL;
}

#if 0
static const char     *pi18n = "i18n.";
static const unsigned  li18n = 5; /* strlen(pi18n) */
#endif

static int search_langset(char *_langset) {
    int   lscnt;  char *curlangset;
#if 0
    DIR * d;  struct dirent *de;

    if ((d = opendir(mi_initrd_cramfs_root "/etc/sysconfig")) == NULL)
	return 0;

    lscnt = 0;  curlangset = _langset;
    while ((de = readdir(d)) != NULL) {
	if (strlen(de->d_name) <= li18n)  continue;
	if (memcmp(de->d_name, pi18n, li18n))  continue;
	curlangset += sprintf(curlangset, "%s:", de->d_name + li18n);
	++lscnt;
    }
    if (curlangset > _langset)  curlangset[-1] = 0; /* Remove the tail : */
    closedir(d);
#else  /* use global variable langset and check if i18n file exists */
    char *lscur;
    struct stat st;
    char fname[255];
    char dup_langset[255];

    lscnt = 0;  curlangset = _langset;
    strcpy(dup_langset, langset);
    for (lscur = strtok(dup_langset, ":"); lscur != NULL; lscur = strtok(NULL, ":")) {
        sprintf(fname, mi_initrd_root "/etc/sysconfig/i18n.%s", lscur);
        if (!stat(fname, &st)) {
            curlangset += sprintf(curlangset, "%s:", lscur);
            ++lscnt;
        }
    }
    if (curlangset > _langset)  curlangset[-1] = 0; /* Remove the tail : */
#endif
    return  lscnt;
}

void langset_saver(FILE *fp) {
    fprintf(fp, "instlang = '%s'\n", instlang);
}
